package com.glodon.paas.account.util.captcha1;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.awt.*;

public class FontCache_Bak {
	static String[] founts = new String[]{"arial","svgafix"};
	static Map<String,Font> baseFontMap = new HashMap<String,Font>();
	static boolean loadedAll = false;
	static int index;
 	/**
 	 * 
 	 * @param fontName arial
 	 * @param style
 	 * @param fontHeight
 	 * @return
 	 */
	public static Font getFont(String fontName, int fontStyle, int fontHeight){
		// 获取指定字体
		Font fount = loadBaseFount(fontName);

		// 获取系统字体
		if(fount != null){
			try {
				fount =  new Font(null,Font.PLAIN, fontHeight);				
			} catch (Exception e) {
			}
		}
		
		// 获取默认字体
		if(fount == null){			
			fount = loadBaseFount(founts[0]);
		}		
		
		return fount.deriveFont(fontStyle, fontHeight);
	} 
	
	// ttf字体文件
	static Font loadBaseFount(String fontName) {
		//随机获取
		if("random".equals(fontName)){
			int t = ++index ;
			t = t % founts.length;
			index = t;
			fontName = founts[t];
		}
		
		Font temp = baseFontMap.get(fontName);
		
		if (temp == null) {
			try {
				URL url = FontCache_Bak.class.getResource(fontName+".ttf");
				if(url == null){
					return null;
				}
				InputStream input = url.openStream();
				temp = Font.createFont(Font.TRUETYPE_FONT, input);
				baseFontMap.put(fontName, temp);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return temp;
	}
	
	static void loadAll(){
		if(loadedAll){
			return;
		}
		loadedAll = true;
		for(String f : founts){
			loadBaseFount(f);			
		}
	}
	
	public static void main(String[] args) throws Exception {
		System.out.println(FontCache_Bak.loadBaseFount("cc"));
		//Arial 
//		GraphicsEnvironment g = GraphicsEnvironment
//		.getLocalGraphicsEnvironment();
//		String fontName[] = g.getAvailableFontFamilyNames();
//		for (int i = 0; i < fontName.length; i++) {
//			System.out.println(fontName[i]);
//		}
	}
}
